package com.chuangke.elasticsearch.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chuangke.common.http.HttpResult;
import com.chuangke.elasticsearch.model.ElasticIdx;

/**
 * @ClassName ElasticIndexController
 * @Description ElasticSearch索引的基本管理，提供对外查询、删除和新增功能
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/elastic")
public class ElasticIndexController {

	private static final Logger logger = LoggerFactory.getLogger(ElasticIndexController.class);

//	@Autowired
//	ElasticsearchService baseElasticService;

	/**
	 * @Description 创建Elastic索引
     */
	@PostMapping(value = "/createIndex")
	public HttpResult createIndex(@RequestBody ElasticIdx elasticIdx) {
		// 索引不存在，再创建，否则不允许创建
//		if (baseElasticService.isExistsIndex(elasticIdx.getIdxName())) {
//			return HttpResult.error("索引已经存在，不允许创建");
//		}

//		String idxSql = JSON.toJSONString(elasticIdx.getIdxSql());
//		logger.warn(" idxName={}, idxSql={}", elasticIdx.getIdxName(), idxSql);
//		baseElasticService.createIndex(elasticIdx.getIdxName());
		return HttpResult.ok();
	}

	/**
	 * @Description 判断索引是否存在；存在-TRUE，否则-FALSE
	 * @param index
	 */
	@GetMapping(value = "/exist/{index}")
	public HttpResult indexExist(@PathVariable(value = "index") String index) {
//		if (!baseElasticService.isExistsIndex(index)) {
//			return HttpResult.ok("索引不存在", index);
//		} else {
//			return HttpResult.ok(" 索引已经存在, " + index);
//		}
		return HttpResult.ok(" 索引已经存在, " + index);
	}

	@GetMapping(value = "/del/{index}")
	public HttpResult indexDel(@PathVariable(value = "index") String index) {
//		baseElasticService.deleteIndex(index);
		return HttpResult.ok();
	}
}
